package com.wanho.qz103.hrsaas.sys.dao.impl;

import com.wanho.qz103.hrsaas.commons.annontations.Repository;
import com.wanho.qz103.hrsaas.commons.dao.impl.BaseDAOImpl;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.DataAccessException;
import com.wanho.qz103.hrsaas.sys.dao.PermissionDAO;
import com.wanho.qz103.hrsaas.sys.pojos.Permission;

import java.util.List;

@Repository
public class PermissionDAOImpl extends BaseDAOImpl<Permission> implements PermissionDAO {

    @Override
    public List<Permission> selectByRoleId(String id) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "\t\tpermId id\n" +
                    "\tFROM\n" +
                    "\t\tsys_role_permission \n" +
                    "WHERE\n" +
                    "\troleId =?" ;
            Object[] paramAy = {id} ;
            return jdbcTemplate.queryList(sql,Permission.class,paramAy);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("根据权限id查询权限集合异常！！！"+e.getMessage()) ;
        }
    }

    @Override
    public List<Permission> selectByUserId(String userId) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "\tid,\n" +
                    "\tCODE,\n" +
                    "\tNAME,\n" +
                    "\tdescription,\n" +
                    "\tenVisible,\n" +
                    "\ttype,\n" +
                    "\tpid \n" +
                    "FROM\n" +
                    "\tsys_permission \n" +
                    "WHERE\n" +
                    "\tid IN (\n" +
                    "\tSELECT\n" +
                    "\t\tpermId \n" +
                    "\tFROM\n" +
                    "\t\tsys_role_permission \n" +
                    "WHERE\n" +
                    "\troleId IN ( SELECT roleId FROM sys_user_role WHERE userId = ? ))" ;
            Object[] paramAy = {userId} ;
            return jdbcTemplate.queryList(sql,Permission.class,paramAy);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("根据用户id查询权限集合异常！！！"+e.getMessage()) ;
        }
    }



    @Override
    protected String deleteSQL() {
        return null;
    }

    @Override
    protected String insertSQL() {
        return null;
    }

    @Override
    protected String updateSQL() {
        return null;
    }

    @Override
    protected String selectByIdSQL() {
        return null;
    }

    @Override
    protected String selectAllSQL() {
        return "SELECT\n" +
                "\tid,\n" +
                "\tCODE,\n" +
                "\tNAME,\n" +
                "\tdescription,\n" +
                "\tenVisible,\n" +
                "\ttype,\n" +
                "\tpid \n" +
                "FROM\n" +
                "\tsys_permission \n" ;
    }
}
